package com.juyu.miliao.api.service;

import com.juyu.miliao.api.dao.mysql.UserInfoDao;
import com.juyu.miliao.api.dao.redis.UserRedisDao;
import com.juyu.miliao.api.domain.TalkingSkill;
import com.juyu.miliao.api.domain.User;
import com.juyu.miliao.api.service.cache.TalkingSkillCacheService;
import com.juyu.miliao.common.constant.EnumType;
import com.juyu.miliao.common.netease.NetEaseCloudUserService;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @Auther: 卜加超
 * @Date: 2019/4/10 15:47
 * @Description:
 */
@Service
@Slf4j
public class TalkingSkillService {
    @Resource
    private TalkingSkillCacheService talkingSkillCacheService;
    @Resource
    private UserInfoDao userInfoDao;
    @Resource
    private UserRedisDao userRedisDao;

    public void sendTalking(long outId, long insertId,int num) {
        User outUser = userInfoDao.getUserById(outId);
        if (outUser.getIsHost() == 2) {
            User insertUser = userInfoDao.getUserById(insertId);
            if (insertUser.getIsHost() == 1 && insertUser.getSex()==2) {
                List<TalkingSkill> list = talkingSkillCacheService.getList();
                List<TalkingSkill> list1 = new ArrayList<>();
                for (TalkingSkill talkingSkill : list) {
                    if (talkingSkill.getId() != 1) {
                        list1.add(talkingSkill);
                    }
                }
                //发送第一条提示话术
                if (num == 1) {
                    String message = list.get(0).getMessage();

                    JSONObject body = new JSONObject();
                    body.put("msg", message);
                    JSONObject ext = new JSONObject();
                    ext.put("first", list.get(0).getId());
                    try {
                        String temp = NetEaseCloudUserService.getSendMsg(outId, insertId,
                                EnumType.TEXT_MESSAGE.state, body.toString(), ext.toString(), "");
                        log.info("第一条发送成功：message={}",message);
                        if (!temp.substring(temp.indexOf("code") + 6, temp.indexOf("code") + 9).equals("200")) {
                            log.error("发送给用户消息异常-云信：userId={},message={}", outId + insertId, temp);
                        }
                    } catch (Exception e) {
                        log.error("发送给用户消息消息异常：userId={},error={}", insertId, e);
                    }
                    //发送第二条提示话术
                } else if (num == 3) {
                    Random random = new Random();
                    TalkingSkill talkingSkill = list1.get(random.nextInt(list1.size()));
                    String message1 = talkingSkill.getMessage();
                    JSONObject body1 = new JSONObject();
                    body1.put("msg", message1);
                    JSONObject ext = new JSONObject();
                    ext.put("two", talkingSkill.getId());
                    try {
                        String temp = NetEaseCloudUserService.getSendMsg(outId, insertId,
                                EnumType.TEXT_MESSAGE.state, body1.toString(), ext.toString(), "");
                        log.info("第二条发送成功：message1={}",message1);
                        if (!temp.substring(temp.indexOf("code") + 6, temp.indexOf("code") + 9).equals("200")) {
                            log.error("发送给用户消息异常-云信：userId={},message={}", outId + insertId, temp);
                        }
                    } catch (Exception e) {
                        log.error("发送给用户消息消息异常：userId={},error={}", insertId, e);
                    }
                }

            }
        }


    }
}
